package com.hsc.www;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImp implements UserService {


    List<User> userList = new ArrayList<>();


    public UserServiceImp() {
        userList.add(new User(1,"dog1",10,"男"));
        userList.add(new User(2,"dog2",10,"女"));
        userList.add(new User(3,"dog3",10,"男"));
        userList.add(new User(4,"dog4",10,"女"));
        userList.add(new User(5,"dog5",10,"男"));
        userList.add(new User(6,"dog6",10,"女"));
    }

    @Override
    public int insertUser(User user) {
        if (user.getId()!=null){
            for (User u:userList) {
                if (u.getId().equals(user.getId())){
                    return 0;
                }
            }
            userList.add(user);
            return 1;
        }else{
            return 0;
        }
    }


    @Override
    public List<User> findAllUsers() {
        return userList;
    }

    @Override
    public User findUserById(int id) {
        for (User u:userList) {
            if (u.getId().equals(id)){
                return  u;
            }
        }
        return null;
    }

    @Override
    public int updateUserById(User user) {
        for (User u:userList) {
            if (u.getId().equals(user.getId())){
                u.setName(user.getName());
                u.setAge(user.getAge());
                u.setSex(user.getSex());
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int  deleteUserById(int id) {
        for (int i = 0;i  < userList.size(); i++) {
            if (userList.get(i).getId().equals(id)){
                userList.remove(i);
                return 1;
            }
        }
        return 0;
    }
}


